package com.briup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.domain.Category;
import com.briup.domain.extend.CategoryExtend;
import com.briup.exception.ServiceException;
import com.briup.mapper.ArticleMapper;
import com.briup.resuit.ResultCode;
import com.briup.service.CategoryService;
import com.briup.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
* @author 白宇杰
* @description 针对表【cms_category】的数据库操作Service实现
* @createDate 2024-11-21 10:24:35
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    ArticleMapper articleMapper;
    @Override
    public void insertCategory(Category category) {
        if (category==null){
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(Category::getName, category.getName());
        Category c = categoryMapper.selectOne(qw);
        if (c!=null){
            throw new ServiceException(ResultCode.FENCE_HOUSE_IS_EXIST);
        }
        Integer parentId = category.getParentId();
        if (parentId!=null&&getBaseMapper().selectById(parentId)==null){
            throw new ServiceException(ResultCode.DATA_CAN_NOT_FATHER);
        }
        int order_num = 1;
        if (categoryMapper.selectCount(null)!=0){
            order_num= categoryMapper.getMaxOrderNum()+1;
        }
        category.setOrderNum(order_num);
        categoryMapper.insert(category);
    }

    @Override
    public void updateCategory(Category category) {
        Integer id=category.getId();
        Category category1 = categoryMapper.selectById(id);
        if (category1==null||id==null){
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getName, category.getName());
        if (category.getName() != null && !category.getName().equals(category1.getName())) {
            lqw.eq(Category::getName, category.getName());
            if (categoryMapper.selectOne(lqw) != null)
                throw new ServiceException(ResultCode.FENCE_HOUSE_IS_EXIST);
        }
        Integer parentId = category.getParentId();
        if (category1.getParentId() == null && parentId != null) {
            throw new ServiceException(ResultCode.DATA_CAN_NOT_FATHER);
        }
        if (category1.getParentId() != null && parentId != null) {
            Category pCategory = categoryMapper.selectById(parentId);
            if (pCategory == null || pCategory.getParentId() != null)
                throw new ServiceException(ResultCode.DATA_CAN_NOT_FATHER);
        }
        categoryMapper.updateById(category);
    }

    @Override
    public void deleteCategory(Integer id) {
        Category category = categoryMapper.selectById(id);
        if (category == null){
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }
        if (category.getParentId() == null){
            LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Category::getParentId, id);
            List<Category> categories = categoryMapper.selectList(lqw);
            if (!categories.isEmpty()){
                throw new ServiceException(ResultCode.DATA_CAN_NOT_DELETE);
            }else {
                Integer num = articleMapper.getArticleNumByCategoryId(id);
                if (num>0){
                    throw new ServiceException(ResultCode.DATA_CAN_NOT_DELETE);
                }
            }
        }
        categoryMapper.deleteById(id);
    }

    @Override
    public void deleteInBatch(List<Integer> ids) {

    }

    @Override
    public IPage<Category> selectAll(Integer pageNum, Integer pageSize, Integer parentId) {
        IPage<Category> iPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Category> qw = new
                LambdaQueryWrapper<>();
        qw.eq(parentId != null, Category::getParentId, parentId);
        qw.orderByAsc(Category::getParentId).orderByAsc(Category::getOrderNum);
        return categoryMapper.selectPage(iPage, qw);
    }

    @Override
    public List<CategoryExtend> queryAllParent() {
        return categoryMapper.queryAllWithCates();
    }
    @Override
    public List<Category> queryAllOneLevel() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(Category::getParentId);
        return categoryMapper.selectList(queryWrapper);
    }

    @Override
    public Category selectOneById(Integer id) {
        return categoryMapper.selectById(id);
    }

}




